//
//  MSSetting.m
//  MS
//
//  Created by He Yuan on 13-2-6.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSSetting.h"

NSString * const ThemeDidChangeNotification = @"me.ilvu.theme.change";

NSString * const SignInTypeForCustomer = @"customer";

NSString * const SignInTypeForHairDresser = @"hairdresser";

NSString * const User_AccountID = @"com.ms.user_accountid";

@implementation MSSetting
@synthesize currentSignInType;

+ (MSSetting *)sharedSetting
{
    static MSSetting *_sharedSetting = nil;
    //创建一个单例
    static dispatch_once_t onceSingleton;
    
    dispatch_once(&onceSingleton, ^{
        _sharedSetting = [[MSSetting alloc] init];
    });
    return _sharedSetting;
}

- (id)init
{
    self = [super init];
    if (self)
    {
        //默认为顾客登陆版本界面
        self.currentSignInType = SignInTypeCustomer;
    }
    return self;
}


-(void)storeTheme:(NSString *)theme
{
    if (theme)
    {
        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
        [def setValue:theme forKey:@"me.ms.theme"];
        [def synchronize];
        [[NSNotificationCenter defaultCenter]
         postNotificationName:ThemeDidChangeNotification
         object:nil];
    }
}

-(NSString *)getTheme
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    
    if ([def objectForKey:@"me.ms.theme"])
    {
        return [def objectForKey:@"me.ms.theme"];
    }
    return kThemeRed;
}

- (UIImage *)imageWithImageName:(NSString *)imageName
{
    NSString *directory = [NSString stringWithFormat:@"/%@/%@", @"style",[self getTheme]];
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName
                                                          ofType:@"png"
                                                     inDirectory:directory];
    return [UIImage imageWithContentsOfFile:imagePath];
}

-(UIColor *)themeTitleColor
{
    if ([[self getTheme] isEqualToString:kThemeGray])
    {
        return [UIColor blackColor];
    }
    else if([[self getTheme] isEqualToString:kThemeRed])
    {
        return [UIColor whiteColor];
    }
    return [UIColor whiteColor];
}

-(UIColor *)themeBackButtonTitleColor
{
    if ([[self getTheme] isEqualToString:kThemeGray])
    {
        return [UIColor darkGrayColor];
    }
    else if([[self getTheme] isEqualToString:kThemeRed])
    {
        return [UIColor whiteColor];
    }
    return [UIColor whiteColor];
}

-(NSArray *)homePageBottomViewConfig
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HomePageBottomViewConfig" ofType:@"plist"];
    NSDictionary *configInfo = [[NSDictionary alloc]initWithContentsOfFile:filePath];
    if (self.currentSignInType == SignInTypeCustomer)
    {
        return [configInfo objectForKey:SignInTypeForCustomer];
    }
    else if(self.currentSignInType == SignInTypeHairDresser)
    {
        return [configInfo objectForKey:SignInTypeForHairDresser];
    }
    return nil;
}


-(void)setDeviceToken:(NSString *)deviceToken
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

    if (deviceToken == nil)
    {
        [def removeObjectForKey:@"deviceToken"];
    }
    else
    {
        [def setObject:deviceToken forKey:@"deviceToken"];
    }
    [def synchronize];
}

-(NSString*)getDeviceToken
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *deviceToken = [def objectForKey:@"deviceToken"];
	return deviceToken;
}

-(void)setAccountID:(NSString *)accountID
{
	if (accountID == nil) return;
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
	[def setObject:accountID forKey:User_AccountID];
}

-(NSString *)getAccountID
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
	return [def objectForKey:User_AccountID];
}

-(NSString *)getGid
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSDictionary *info = [def objectForKey:User_AccountInfo];
    NSString *gid = @"";
    if (info) {
        gid = [info objectForKey:@"gid"];
    }
    if (!gid)gid = @"";
    return gid;
}

-(void)setAccountInfo:(NSDictionary *)accountinfo
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    
    if (accountinfo == nil)
        [def removeObjectForKey:User_AccountInfo];
    else
        [def setObject:accountinfo forKey:User_AccountInfo];
    
    [def synchronize];
}

-(NSDictionary *)getAccountInfo
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
	return [def objectForKey:User_AccountInfo];
}


-(void)resetUserInfo
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud removeObjectForKey:User_AccountID];
    [ud removeObjectForKey:User_AccountName];
    [ud removeObjectForKey:User_AccountPassword];
    [ud removeObjectForKey:User_AccountInfo];
    [ud synchronize];
}


@end
